<?php


namespace app\impl;


use app\common\lib\ZhyxMongo;
use app\interfaces\Task;

class SyncOutHospital implements Task
{

    public function run(array $data,string $taskId): ?bool
    {
        // TODO: Implement run() method.
        $startDate = $data["startDate"] ?? date("Y-m-d", strtotime("-1 day"));
        $endDate = $data["endDate"] ?? date("Y-m-d", strtotime("-1 day"));
        $startDate = $endDate = "2021-08-01";
        try {

            //$data = HisData::getHisData($data["type"], $startDate, $endDate);
            $data = json_decode(file_get_contents("./chuyuan.txt"), true);
            foreach ($data as $k => $v) {
                //判断住院号与ID是否存在在患者节点中，如果不存在则进行插入操作
                $v["deleted_at"] = 0;
                $v["created_at"] = time();
                //判断当前患者节点中是否已经存在该记录如果存在则跳出
                $isExists = ZhyxMongo::getInstance()->setCollection("patient_record")->findOne(["no" => $v["inhospitalNo"], "summary.id" => $v["id"]]);
                if (empty($isExists)) {
                    $result = ZhyxMongo::getInstance()->setCollection("patient_record")->updateOne(["no" => $v["inhospitalNo"]], ['$push' => [
                        'summary' => $v
                    ]]);
                }


            }
        } catch (\Exception $exception) {
            echo $exception->getMessage();
            exit();
            \think\facade\Log::record("同步出院小节信息异常:" . $exception->getMessage(), "error");
        }

        return true;
    }
}